package com.example.hearthstoneagain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.os.Build;

public class MainBuscaActivity extends ActionBarActivity implements SearchView.OnQueryTextListener {
	
	public static MainBuscaActivity instancia;
	
	Context context;
	
	ListView lst;
	
	SearchView searchView;
	
	List<HashMap<String,String>> listacartas;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_busca);

		instancia = this;
		
		context = this.getApplicationContext();
		
		BancodeDados banco = new BancodeDados(context);
		
		//banco.onUpgrade(banco.getWritableDatabase(),1,2);
		
		SQLiteDatabase dtb = banco.getReadableDatabase();
		
		// cria lista nome de cartas pro list view
		
		String nomecol[] = {"name","cost"};
		
		Cursor resp_nome_cards = dtb.query("cartas", nomecol, null, null, null, null, "name ASC");
		
		resp_nome_cards.moveToFirst();
		
		listacartas = new ArrayList<HashMap<String,String>>();
		
		for(int i=0;i<Cartas.numCartas;i++) {
			HashMap<String, String> hm = new HashMap<String,String>();
			hm.put("txt", resp_nome_cards.getString(0));
			int custodemana = resp_nome_cards.getInt(1);
			int pic=0;
		
			switch(custodemana) {
				case 0:
					pic = R.drawable.custo0;
					break;
				case 1:
					pic = R.drawable.custo1;
					break;
				case 2:
					pic = R.drawable.custo2;
					break;
				case 3:
					pic = R.drawable.custo3;
					break;
				case 4:
					pic = R.drawable.custo4;
					break;
				case 5:
					pic = R.drawable.custo5;
					break;
				case 6:
					pic = R.drawable.custo6;
					break;
				case 7:
					pic = R.drawable.custo7;
					break;
				case 8:
					pic = R.drawable.custo8;
					break;
				case 9:
					pic = R.drawable.custo9;
					break;
				case 10:
					pic = R.drawable.custo10;
					break;
				case 12:
					pic = R.drawable.custo12;
					break;
				case 20: 
					pic = R.drawable.custo20;
					break;
				default:
					pic = R.drawable.nocost;		
			}
			
			hm.put("flag", Integer.toString(pic));
			resp_nome_cards.moveToNext();
			listacartas.add(hm);
		}
		
		lst = (ListView) this.findViewById(R.id.listView3);
		
		searchView = (SearchView) this.findViewById(R.id.searchView1);
		
		String[] from = { "flag","txt" };

		int[] to = { R.id.flag,R.id.txt};
		
		dtb.close();
		
		CustomAdapter adapter = new CustomAdapter(getBaseContext(), listacartas, R.layout.listview_layout, from, to);
		
		lst.setAdapter(adapter);
		
		lst.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				
				BancodeDados banco = new BancodeDados(context);
				
				SQLiteDatabase dtb = banco.getReadableDatabase();
				
				HashMap<String, String> hm = (HashMap<String, String>) lst.getAdapter().getItem(arg2);
				
				String nomecarta = hm.get("txt");
				
				String cols[] = {"texto","attack","health","cost","tipo","comentario","url"};
				
				String args[] = {nomecarta};
				
				Cursor resp_nome_clicado = dtb.query("cartas",cols, "name = ?", args, null, null, null);
				
				resp_nome_clicado.moveToFirst();
				
				Intent intent = new Intent (MainBuscaActivity.this,CartaActivity.class);
				intent.putExtra("nome", nomecarta);
				intent.putExtra("textocarta", resp_nome_clicado.getString(0));
				intent.putExtra("attack", resp_nome_clicado.getInt(1));
				intent.putExtra("health", resp_nome_clicado.getInt(2));
				intent.putExtra("custo", resp_nome_clicado.getInt(3));
				intent.putExtra("tipo", resp_nome_clicado.getString(4));
				intent.putExtra("comentario", resp_nome_clicado.getString(5));
				intent.putExtra("url", resp_nome_clicado.getString(6));
					
				dtb.close();
				
				TabGroupActivity parentActivity = (TabGroupActivity)getParent();
				parentActivity.startChildActivity("CartaActivity", intent);
				
				//startActivity(intent);
			}
		});
		
		lst.setTextFilterEnabled(true);
		
		searchView.setOnQueryTextListener(this);
		
	}

	@Override
	public boolean onQueryTextChange(String arg0) {
		if (TextUtils.isEmpty(arg0)) {
            lst.clearTextFilter();
        } else {
            lst.setFilterText(arg0.toString());
            if(arg0.toString().equals("bobera")) {
            	Intent intent = new Intent (MainBuscaActivity.this,CartaActivity.class);
				intent.putExtra("nome", "bobeira");
				TabGroupActivity parentActivity = (TabGroupActivity)getParent();
				parentActivity.startChildActivity("CartaActivity", intent);
            }
        }
		return true;
	}

	@Override
	public boolean onQueryTextSubmit(String arg0) {
		return false;
	}
	
	public void abreCarta (String carta) {
		
		BancodeDados banco = new BancodeDados(context);
		
		SQLiteDatabase dtb = banco.getReadableDatabase();
		
		String cols[] = {"texto","attack","health","cost","tipo","comentario","url"};
		
		String args[] = {carta};
		
		Cursor resp_nome_clicado = dtb.query("cartas",cols, "name = ?", args, null, null, null);
		
		resp_nome_clicado.moveToFirst();
		
		Intent intent = new Intent (MainBuscaActivity.this,CartaActivity.class);
		intent.putExtra("nome", carta);
		intent.putExtra("textocarta", resp_nome_clicado.getString(0));
		intent.putExtra("attack", resp_nome_clicado.getInt(1));
		intent.putExtra("health", resp_nome_clicado.getInt(2));
		intent.putExtra("custo", resp_nome_clicado.getInt(3));
		intent.putExtra("tipo", resp_nome_clicado.getString(4));
		intent.putExtra("comentario", resp_nome_clicado.getString(5));
		intent.putExtra("url", resp_nome_clicado.getString(6));
			
		dtb.close();
		
		TabGroupActivity parentActivity = (TabGroupActivity)getParent();
		parentActivity.startChildActivity("CartaActivity", intent);
	}

}
